<?php
/**
 * Created by PhpStorm.
 * User: yaoihu
 * Date: 2019/4/23
 * Time: 21:14
 */

namespace App\Http\Controllers\Shop;


use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\Shop\ShopTypeModel;
use Illuminate\Http\Request;

class ShopTypeController extends BackendController
{
    /**
     * @title 商品类型管理
     * @param Request $request
     * @return View
     */
    public function types(Request $request)
    {
        $list = ShopTypeModel::with(['attributes','specs'])->paginate($this->getPerPageNum());
        return view('backend::shop.type.types_list',[
            'page_title'=>'商品类型',
            'list'=>$list
        ]);
    }

    /**
     * @title 添加商品类型
     * @param Request $request
     * @return SelfResponse|View
     */
    public function create(Request $request)
    {
        if($request->isMethod('post')){
            $typeModel = new ShopTypeModel();
            $typeModel->name = $request->post('name');
            $typeModel->description = $request->post('description');
            if(!$typeModel->save()){
                return SelfResponse::error('添加失败');
            }
            return SelfResponse::success('添加成功','b_ShopType_types');
        }else{
            return view('backend::shop.type.type_add_or_edit',[
                'page_title'=>'添加商品类型'
            ]);
        }
    }

    /**
     * @title 编辑商品类型
     * @param Request $request
     * @param $id
     * @return SelfResponse|View
     */
    public function update(Request $request, $id)
    {
        $info = ShopTypeModel::find($id);
        if($request->isMethod('post')){
            if(!$info){
                return SelfResponse::error('修改的类型不存在');
            }
            $info->name = $request->post('name');
            $info->description = $request->post('description');
            if(!$info->save()){
                return SelfResponse::error('编辑失败');
            }
            return SelfResponse::success('编辑成功','b_ShopType_types');
        }else{
            if(!$info){
                return redirect()->route('b_ShopType_create');
            }
            return view('backend::shop.type.type_add_or_edit',[
                'page_title'=>'编辑'.$info->name.'类型',
                'info'=>$info
            ]);
        }
    }

    /**
     * @title 删除商品类型
     * @param $id
     * @return SelfResponse|RedirectResponse
     */
    public function delete($id)
    {
        $info = ShopTypeModel::find($id);
        if(!$info){
            return SelfResponse::error('修改的类型不存在');
        }
        if(!$info->delete()){
            return SelfResponse::error('删除失败');
        }
        return SelfResponse::success('删除成功','b_ShopType_types');
    }

    /**
     * @title 批量删除商品类型
     * @param Request $request
     * @return SelfResponse
     */
    public function batchDelete(Request $request)
    {
        $ids = array_filter($request->input('ids'));
        if (!$ids){
            return SelfResponse::error('请选择要删除的数据');
        }

        if(!ShopTypeModel::whereIn('id',$ids)->delete()){
            return SelfResponse::error('批量删除失败');
        }
        return SelfResponse::success('批量删除成功','b_ShopType_types');
    }
}